namespace Vanilla.UI

import System

partial class Gadget:
	// MOUSE BUTTON STATUS
	public static GadgetWithLeftMouseDown as Gadget = null
	public static GadgetWithRightMouseDown as Gadget = null
	
	public static LeftMouseDownPosition as Position
	
	LeftMouseDownInside as bool:
		get:
			return self == GadgetWithLeftMouseDown
	
	RightMouseDownInside as bool:
		get:
			return self == GadgetWithRightMouseDown
	
	// EVENTS
	virtual def MouseDown (b as MouseButton, x as double, y as double):
		if b == MouseButton.Left:
			GadgetWithLeftMouseDown = self
			LeftMouseDownPosition = Position(x,y)
		elif b == MouseButton.Right:
			GadgetWithRightMouseDown = self
		//print "${GetType()}.MouseDown(${b}, ${x}, ${y})"
	
	virtual def MouseUp (b as MouseButton, x as double, y as double):
		//if self isa Panel:
		print "${GetType()} mouse up. ${GadgetBeingDragged} being dragged"
		if (b == MouseButton.Left and LeftMouseDownInside) or (b == MouseButton.Right and RightMouseDownInside):
			Click(b,x,y)
		elif GadgetBeingDragged != null:
			//if AcceptsDrop(GadgetBeingDragged, x, y):
			Drop(GadgetBeingDragged,x,y)
			CleanUpAndEndDrag()
			//else:
			//	GadgetBeingDragged.UndoDragFrom(ParentOfGadgetBeingDragged)
		//g = "null"
		//g = "${GadgetBeingDragged.GetType()}" unless GadgetBeingDragged is null
		//print "${GetType()}.MouseUp(${b}, ${x}, ${y}) AcceptsDrops=${AcceptsDrops} AcceptsDrop(g, ${x}, ${y})=${AcceptsDrop(GadgetBeingDragged, x, y)}"
	
	virtual def MouseEnter ():
		//print "${GetType()}.MouseEnter() inside=${ContainsMouse} wasinside=${PreviouslyContainedMouse}"
		Redraw() if Theme.MouseOverChangesLookFor(self)
	
	virtual def MouseLeave ():
		//print "${GetType()}.MouseLeave() inside=${ContainsMouse} wasinside=${PreviouslyContainedMouse}"
		Redraw() if Theme.MouseOverChangesLookFor(self)
	
	virtual def MouseMotion (x as double, y as double):
		//print "${GetType()}.MouseMotion(${x}, ${y}) LMDI=${LeftMouseDownInside} GWLMD=${GadgetWithLeftMouseDown != null} CanDrag=${CanDrag}"
		//if LeftMouseDownInside and CanDrag and GadgetBeingDragged is null:
		//	if x < LeftMouseDownPosition.X-10 or x > LeftMouseDownPosition.X+10 or y < LeftMouseDownPosition.Y-10 or y > LeftMouseDownPosition.Y+10:
		//		Drag()
		pass
	
	virtual def Click (b as MouseButton, x as double, y as double):
		pass
		//print "---CLICK--- ${GetType()}"
		//FocusedGadget = self
	
	virtual def DoubleClick (b as MouseButton, x as double, y as double):
		pass
	
	// FOCUS
	public static GadgetUnderMouse as Gadget = null
	
	public static FocusedGadget as Gadget:
		get:
			return GadgetUnderMouse
	
	public static PreviousGadgetUnderMouse as Gadget = null
	public static GadgetWithMouseGrabbed as Gadget = null
	
	ContainsMouse as bool:
		get:
			return self == GadgetUnderMouse or Contains(GadgetUnderMouse)
	
	PreviouslyContainedMouse as bool:
		get:
			return self == PreviousGadgetUnderMouse or Contains(PreviousGadgetUnderMouse)
	
	IsFocused as bool:
		get:
			return self == FocusedGadget or Contains(FocusedGadget)
	
	def GrabMouse ():
		GadgetWithMouseGrabbed = self
	
	def ReleaseMouse ():
		GadgetWithMouseGrabbed = null if GadgetWithMouseGrabbed == self
	
	ContainsMouseGrabber as bool:
		get:
			return GadgetWithMouseGrabbed == self or Contains(GadgetWithMouseGrabbed)
